package jumpingnotes.restlet.resource;

import static jumpingnotes.restlet.ErrorCode.NO_PERMISSION;
import static jumpingnotes.restlet.ErrorCode.OBJECT_NOT_FOUND;
import static jumpingnotes.restlet.ErrorCode.SERVER_INTERNAL_ERROR;

import java.util.ArrayList;
import java.util.List;

import jumpingnotes.model.entity.Activity;
import jumpingnotes.model.entity.Member;
import jumpingnotes.restlet.JsonMapping;

import org.json.JSONObject;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;

/* Activities of all the groups the member belongs to. */
public class AllGroupMemberActivitiesResource extends BaseResource
{
	private Member member = null;
	private int startIndex = 0;
	private int count = 10;

	@Override
	public void init(Context context, Request request, Response response)
	{
		super.init(context, request, response);

		getVariants().add(new Variant(MediaType.APPLICATION_JSON));

		try
		{
			sessionId = (String) getRequest().getAttributes().get("sessionId");
			checkPermission();

			if (hasPermission())
			{
				// URL:
				// "/members/{memberId}/groupactivities?startIndex={startIndex}&amp;count={count}"
				String memberIdStr = (String) getRequest().getAttributes().get("memberId");

				String startIndexStr = (String) getRequest().getAttributes().get("startIndex");
				String countStr = (String) getRequest().getAttributes().get("count");

				startIndex = Integer.valueOf(startIndexStr);
				count = Integer.valueOf(countStr);

				if ("me".equalsIgnoreCase(memberIdStr))
				{
					String email = session.getEmail();
					member = service.findMemberByEmail(email);
				}
				else
				{
					int memberId = Integer.parseInt(memberIdStr);
					member = service.findMemberById(memberId);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public Representation represent(Variant variant) throws ResourceException
	{
		if (!hasPermission())
		{
			return createErrorRepresentation(NO_PERMISSION);
		}

		if (member == null)
		{
			return createErrorRepresentation(OBJECT_NOT_FOUND);
		}

		try
		{
			// get all object
			List<Activity> activityList = service.findActivityByAllGroupMemberId(member.getMemberId(), startIndex, count);

			// get object count
			int activityListSize = service.findActivityByAllGroupMemberId(member.getMemberId(), -1, -1).size();

			// create json object
			List<JSONObject> activityJsonList = new ArrayList<JSONObject>();
			for (Activity activity : activityList)
			{
				JSONObject activityJson = JsonMapping.map(activity);
				activityJsonList.add(activityJson);
			}

			JSONObject activitiesJson = new JSONObject();
			activitiesJson.put("count", activityListSize);
			activitiesJson.put("activities", activityJsonList);

			return createJsonRepresentation(activitiesJson);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return createErrorRepresentation(SERVER_INTERNAL_ERROR);
		}
	}
}
